package gov.pbc.nn.uflo.view;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.bstek.bdf2.core.context.ContextHolder;
import com.bstek.bdf2.core.orm.ParseResult;
import com.bstek.dorado.annotation.DataProvider;
import com.bstek.dorado.annotation.DataResolver;
import com.bstek.dorado.annotation.Expose;
import com.bstek.dorado.data.entity.EntityState;
import com.bstek.dorado.data.entity.EntityUtils;
import com.bstek.dorado.data.provider.Criteria;
import com.bstek.dorado.data.provider.Page;

import gov.pbc.nn.core.dao.PbcnnCoreHibernateDao;
import gov.pbc.nn.core.dao.PbcnnCoreJdbcDao;
import gov.pbc.nn.core.utils.GetTableUtils;
import gov.pbc.nn.uflo.entity.CommonOpinion;

@Component
public class CommonOpinionPR {

	@Resource(name = PbcnnCoreHibernateDao.BEAN_ID)
	private PbcnnCoreHibernateDao hibernateDao;

	@Resource(name = PbcnnCoreJdbcDao.BEAN_ID)
	private PbcnnCoreJdbcDao jdbcDao;

	@DataProvider
	public void loadData(Page<CommonOpinion> page, Criteria criteria, Map<String, Object> params) {
		String username = null;
		if (params != null) {
			username = (String) params.get("username");
		}
		DetachedCriteria dc = this.hibernateDao.buildDetachedCriteria(criteria, CommonOpinion.class);
		dc.add(Restrictions.eq("invalid", false));
		if (StringUtils.isNotBlank(username)) {
			dc.add(Restrictions.eq("type", 1));
			dc.add(Restrictions.eq("username", username));
		} else {
			dc.add(Restrictions.eq("type", 0));
		}
		this.hibernateDao.pagingQuery(page, dc);
	}

	@DataResolver
	@Transactional
	public void saveData(List<CommonOpinion> list) {
		for (CommonOpinion co : list) {
			EntityState state = EntityUtils.getState(co);
			if (state == EntityState.NEW) {
				this.hibernateDao.save(co);
			} else if (state == EntityState.MODIFIED) {
				this.hibernateDao.update(co);
			} else if (state == EntityState.DELETED) {
				this.hibernateDao.delete(co);
			}
		}
	}

	@Transactional
	@Expose
	public void deleteData(String id) {
		this.hibernateDao.delete(id, CommonOpinion.class);
	}

	@DataProvider
	public void loadOpinion(Page<Map<String, Object>> page, Criteria criteria) throws Exception {
		Map<String, Object> parameterMap = new HashMap<String, Object>();
		parameterMap.put("username", ContextHolder.getLoginUserName());
		String sql = "select ID_,OPINION_,TYPE_ from " + GetTableUtils.getTablename(CommonOpinion.class)
				+ " where INVALID_=0 AND (username_ is null or username_=:username)";
		ParseResult result = this.jdbcDao.parseCriteria(criteria, true, null);
		if (result != null) {
			sql += " and " + result.getAssemblySql();
			parameterMap.putAll(result.getValueMap());
		}
		this.jdbcDao.pagingQuery(page, sql, parameterMap);
	}

	@Transactional
	@Expose
	public void saveAsMine(String opinion) {
		CommonOpinion co = new CommonOpinion();
		co.setCname(ContextHolder.getLoginUser().getCname());
		co.setOpinion(opinion);
		co.setUsername(ContextHolder.getLoginUserName());
		co.setType(1);
		this.hibernateDao.save(co);
	}
}
